package com.example.securitydemo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.securitydemo.entity.TabMenu;
import com.example.securitydemo.entity.TabRole;
import com.example.securitydemo.entity.TabUser;
import com.example.securitydemo.exe.MyUserNameNotFoundException;
import com.example.securitydemo.mapper.TabMenuMapper;
import com.example.securitydemo.mapper.TabRoleMapper;
import com.example.securitydemo.mapper.TabUserMapper;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class MyUserService implements UserDetailsService {
    @Resource
    private TabUserMapper userMapper;
    //
    @Resource
    private TabRoleMapper roleMapper;
    @Resource
    private TabMenuMapper menuMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 根据用户名查询用户的信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);// username="1222"
        TabUser tabUser = userMapper.selectOne(queryWrapper);
        System.out.println("用户的信息是:"+tabUser);
        if (tabUser==null){
            System.out.println("当前的用户不存在");
            throw new BadCredentialsException("用户名或者密码错误");
        }else{
            // 用户存在的   密码 密文
            // 对应的资源的信息  1. 角色的信息   2. 资源的信息
            // 根据用户的id  需要查询该用户对应的所有的角色的编码
            Integer id = tabUser.getId();
            List<TabRole> roleList = roleMapper.getRoleByUid(id);
            System.out.println("角色的信息:"+roleList);
            // 角色的code 存放到对应的资源里面
            List<SimpleGrantedAuthority> roles = roleList.stream().map(tabRole -> new SimpleGrantedAuthority("ROLE_"+tabRole.getRcode())).collect(Collectors.toList());
            //根据当前用户的角色的id 资源
            List<Integer> rids = roleList.stream().map(tabRole -> tabRole.getRid()).collect(Collectors.toList());
            // 根据角色的id 查询对应的菜单的信息
            List<TabMenu> menuList =  menuMapper.getMenusByRids(rids);
            System.out.println("当前角色的所有的菜单的信息:"+menuList);
            // 角色的信息   还有资源的信息
            List<SimpleGrantedAuthority> menus = menuList.stream().map(tabMenu -> new SimpleGrantedAuthority(tabMenu.getMname())).collect(Collectors.toList());
            // stream 合并   concat
            List<SimpleGrantedAuthority> authorities = Stream.concat(roles.stream(), menus.stream()).collect(Collectors.toList());
            User user = new User(username,tabUser.getPassword(),authorities);
            return user;
        }


        // username   指代的是 用户在表单里面输入的用户的名字
        // 数据库里面  查询一下用户名是 username的用户的信息
        // 数据库里面  yyl   123456  角色是admin

        // 1. 连接数据库
        //    a. 根据输入的用户名查询用户的信息
        //    存在     查询用户所拥有的角色

        // 分离
        //  认证成功
//        if(!username.equals("yyl")){
//            System.out.println("用户名不存在");
//            throw new MyUserNameNotFoundException("用户名不存在");
//        }else{
//            // 存在
//            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
//            // user ROLE_
//            // 资源    角色 SimpleGrantedAuthority
//            authorities.add(new SimpleGrantedAuthority("user"));//
//            System.out.println(authorities);
//            // List<Si>
//            // 123456 加密-》
//            User user = new User(username,new BCryptPasswordEncoder().encode("123"),authorities);
//
//            return user;
//        }

      //  return null;

    }
}
